package cn.lls.controller.employee;


import cn.lls.base.result.R;
import cn.lls.entity.company.AnnouncementEntity;
import cn.lls.req.emp.ConditionSearchAnnoReq;
import cn.lls.service.IAnnouncementService;
import cn.lls.vo.emp.AnnoInfoVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 划水艺术家
 * @since 2022-11-15
 */
@RestController
@Slf4j
@RequestMapping("/emp/ann")
public class AnnouncementController {

    @Autowired
    private IAnnouncementService announcementService;


    @PostMapping("/info")
    @ApiOperation(value = "分页条件查询本公司公告", notes = "分页条件查询本公司公告", response = R.class, httpMethod = "POST")
    public R getAnnoByConditionAndPage(@RequestBody @Validated ConditionSearchAnnoReq conditionSearchAnnoReq,
                                           BindingResult bindingResult){
        AnnoInfoVO annoInfoVO = new AnnoInfoVO();
        BeanUtils.copyProperties(announcementService.getAnnoByConditionAndPage(conditionSearchAnnoReq), annoInfoVO);
        log.info("anno = {}", annoInfoVO);
        return R.success(annoInfoVO);
    }


    @GetMapping("/{announcementId}")
    public R getAnnById(@PathVariable("announcementId") Long announcementId){

        return R.success(announcementService.getAnnoById(announcementId));
    }


    @PostMapping
    public R saveAnno(@RequestBody @Validated AnnouncementEntity announcementEntity){
        announcementService.save(announcementEntity);
        return R.success();
    }

    @DeleteMapping("/{announcementId}")
    public R deleteAnno(@PathVariable("announcementId") Long announcementId){
        announcementService.removeById(announcementId);
        return R.success();
    }

}

